angular.module('mvcApp.user-admin', [
    'ngRoute',
    'ngResource'
]).config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/user-admin', {
            templateUrl: 'static/views/user-admin.html',
            controller: 'UserAdminCtrl'
        });
}]).controller('UserAdminCtrl', ['$scope', '$rootScope', '$http', '$location', 'User', '$routeParams', 'Shop', 'Category', 'Product',
    function ($scope, $rootScope, $http, $location, User, $routeParams, Shop, Category, Product) {
        $scope.currentPage = 'list';
        $scope.users = [];
        $scope.usersPage = 1;
        $scope.usersTotal = 0;
        $scope.pageSize = 10;
        $scope.toManageUser = function (user) {
            $scope.currentUser = user;
            $scope.currentPage = 'manage-user';
            if ($scope.currentUser.roles) {
                _.each($scope.allRoles, function (role) {
                    for (var i = 0; i < $scope.currentUser.roles.length; ++i) {
                        if ($scope.currentUser.roles[i].name === role.name) {
                            role.checked = true;
                            return;
                        }
                    }
                    role.checked = false;
                });
            }
            $scope.$applyAsync();
        };
        $scope.allRoles = [];
        User.getAllRoles()
            .then(function (roles) {
                $scope.allRoles = roles;
            }).catch(function (err) {
                if (err) {
                    console.log(err);
                }
            });
        $scope.toggleUserRole = _.throttle(function (user, role) {
            var method = role.checked ? 'addRoleToUser' : 'removeRoleFromUser';
            User[method](user.id, role.id)
                .then(function (roles) {
                    user.roles = roles;
                    alert("修改角色完成");
                    $scope.toManageUser(user);
                }).catch(function (err) {
                    alert(err);
                });
        }, 1000);
        $scope.loadUsers = function () {
            if ($scope.usersPage < 1) {
                $scope.usersPage = 1;
            }
            User.listUsers($scope.usersPage, $scope.pageSize)
                .then(function (users, total) {
                    $scope.users = users;
                    $scope.usersTotal = total;
                    $scope.$apply();
                }).catch(function (err) {
                    if (err) {
                        alert(err);
                    }
                });
        };
        $scope.loadUsers();
        $scope.toAddUser = function () {
            $scope.currentPage = 'create';
        };
        $scope.addUser = function (username, password, isShopOwner) {
            if (!username || !password) {
                alert("用户名密码不可为空");
                return;
            }
            User.createUser(username, password, isShopOwner)
                .then(function (data) {
                    alert("添加用户成功");
                    $scope.currentPage = 'list';
                    $scope.loadUsers();
                }).catch(function (e) {
                    alert(e);
                });
        };
    }]);